//
//  QSViewMaker.m
//  Gunz
//
//  Created by Joggy on 16/8/22.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "QSViewMaker.h"
#import "QSViewCell.h"
#import "Step3InputField.h"

@interface QSViewMaker () {
    
    UIWindow *keyWindow;
    QSType type;
    NSString *titleAdded;
    Step3InputField *qsTitle;
    NSMutableArray *qsViewArray;
    
}

@end

@implementation QSViewMaker

- (instancetype)initWithTitle:(NSString *)title {
    self = [super initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    keyWindow = [UIApplication sharedApplication].keyWindow;
    qsViewArray = [[NSMutableArray alloc] initWithCapacity:4];
    titleAdded = title;
    return self;
}

- (void)showWithSingle {
    type = QSTypeSingle;
    
    UIView *qsViewAddView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width*0.85, 66)];
    qsViewAddView.backgroundColor = CUSTOM_BACKGROUND;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, qsViewAddView.frame.size.width, 66)];
    titleLabel.text = titleAdded;
    titleLabel.textColor = TEXT_TITLE_COLOR;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [qsViewAddView addSubview:titleLabel];
    
    qsTitle = [[Step3InputField alloc] initWithTitle:@"问题:"];
    qsTitle.center = CGPointMake(qsViewAddView.frame.size.width/2, 80);
    [qsViewAddView addSubview:qsTitle];
    for(int i = 0; i < 4; i++) {
        Step3InputField *qsAnswer = [[Step3InputField alloc] initWithTitle:[NSString stringWithFormat:@"%@%d%@", @"答案", i, @":"]];
        qsAnswer.center = CGPointMake(qsViewAddView.frame.size.width/2, 80 + 45*(i + 1));
        [qsViewAddView addSubview:qsAnswer];
        [qsViewArray addObject:qsAnswer];
    }
    CGRect aFrame = qsViewAddView.frame;
    Step3InputField *ans = qsViewArray.lastObject;
    aFrame.size.height = ans.frame.origin.y + ans.frame.size.height + 66;
    qsViewAddView.frame = aFrame;
    qsViewAddView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 - 80);
    [self addSubview:qsViewAddView];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdit)];
    [qsViewAddView addGestureRecognizer:tapGes];
    
    [self addButtonByView:qsViewAddView];
    [self show];
}

- (void)showWithMultiple {
    [self showWithSingle];
    type = QSTypeMultiple;
}

- (void)showWithShortAnswer {
    type = QSTypeShortAnswer;
    UIView *qsViewAddView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width*0.85, 66)];
    qsViewAddView.backgroundColor = CUSTOM_BACKGROUND;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, qsViewAddView.frame.size.width, 66)];
    titleLabel.text = titleAdded;
    titleLabel.textColor = TEXT_TITLE_COLOR;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [qsViewAddView addSubview:titleLabel];
    
    qsTitle = [[Step3InputField alloc] initWithTitle:@"问题:"];
    qsTitle.center = CGPointMake(qsViewAddView.frame.size.width/2, 80);
    [qsViewAddView addSubview:qsTitle];
    
    CGRect aFrame = qsViewAddView.frame;
    aFrame.size.height = qsTitle.frame.origin.y + qsTitle.frame.size.height + 66;
    qsViewAddView.frame = aFrame;
    qsViewAddView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 - 80);
    [self addSubview:qsViewAddView];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdit)];
    [qsViewAddView addGestureRecognizer:tapGes];
    
    [self addButtonByView:qsViewAddView];
    [self show];
}

- (void)show {
    [keyWindow addSubview:self];
}

- (void)addButtonByView:(UIView*)view {
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(0, 0, 88, 44);
    cancelButton.center = CGPointMake(view.frame.size.width/3, view.frame.size.height - 33);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:CUSTOM_GREEN forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:cancelButton];
    UIButton *conformButton = [UIButton buttonWithType:UIButtonTypeSystem];
    conformButton.frame = CGRectMake(0, 0, 88, 44);
    conformButton.center = CGPointMake(view.frame.size.width*2/3, view.frame.size.height - 33);
    [conformButton setTitle:@"确定" forState:UIControlStateNormal];
    [conformButton setTitleColor:CUSTOM_GREEN forState:UIControlStateNormal];
    [conformButton addTarget:self action:@selector(conform) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:conformButton];
}

- (void)conform {
    NSDictionary *userInfo;
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
    switch (type) {
        case QSTypeSingle:
            for (Step3InputField *field in qsViewArray) {
                [arr addObject:field.textField.text];
            }
            userInfo = @{
                         @"answer": arr,
                         @"question": qsTitle.textField.text,
                         @"type": @"1"
                         };
            break;
        case QSTypeMultiple:
            for (Step3InputField *field in qsViewArray) {
                [arr addObject:field.textField.text];
            }
            userInfo = @{
                         @"answer": arr,
                         @"question": qsTitle.textField.text,
                         @"type": @"2"
                         };
            break;
        case QSTypeShortAnswer:
            userInfo = @{
                         @"question": qsTitle.textField.text,
                         @"type": @"3"
                         };
            break;
        default:
            userInfo = @{@"type": @"-1"};
            break;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:QSViewCellDataNotification object:nil userInfo:userInfo];
    [self dismiss];
}

- (void)endEdit {
    [self endEditing:true];
}

- (void)dismiss {
    [self removeFromSuperview];
}

@end
